import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'

// px转vw
import pxToViewport from 'postcss-px-to-viewport'
// 自动添加css浏览器前缀，增强兼容性
import autoPreFixer from 'autoprefixer'
// 按需引入vant
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    Components({
      resolvers: [VantResolver()],
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  css: {
    postcss: {
      plugins: [
        autoPreFixer({
          overrideBrowserslist: [
            'Android 4.1',
            'iOS 7.1',
            'Chrome > 31',
            'ff > 31',
            '> 1%'
          ],
          grid: true
        }),
        pxToViewport({
          viewportWidth: 750,
          unitPrecision: 3,
          // vant组件不转换
          selectorBlackList: ['van-', 'no2vw-'],
          exclude: [
            // src/assets/style/reset.css 不转换
            /src\/assets\/style\/reset.css/,
            // 外部库不转换
            /node_modules/
          ]
        })
      ]
    }
  }
})
